ManagementObjectSearcher error [migrated]

Posted by Piotrek on Programmers See other posts from Programmers or by Piotrek
Published on 2012-03-27T12:26:26Z Indexed on 2012/03/27 17:43 UTC
Read the original article Hit count: 246

Filed under:

Some of our customers inform us that in some cases following error appears:

System.Management.ManagementException: Blad dostawcy.
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()

The error is generated while trying to loop through a colection returned by Get() method of the System.Mamangment.ManagementObjectSearcher object.

This is the code of my method:

private bool PrinterExists(string printerName)
{
    bool retVal = false;
    SelectQuery q = new SelectQuery("select caption from win32_printer");
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(q))
    {
        foreach (ManagementObject printer in searcher.Get())
        {
            if(printer["Caption"].ToString() == printerName)
            {
                retVal = true;
                break;
            }
        }
    }
    return retVal;
}

It seems that the problem appears only on Windows XP. The only workaround I know is reconstruction of WMI database. It sometimes helps, but unfortunatelly not always.

Can anyone tell me what is the reason of this error and how can I fix it?

© Programmers or respective owner

Related posts about c#